package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.SpecialColumnComponent;
import com.go.where.family.constants.Constant;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.SpecialColumnInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.SpecialColumnService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.SpecialColumnVo;
import com.google.gson.Gson;

@Component("specialColumnComponent")
public class SpecialColumnComponentImpl implements SpecialColumnComponent {

	private static final Logger log = Logger
			.getLogger(SpecialColumnComponentImpl.class);
	private Gson gson;
	@Resource(name = "specialColumnService")
	private SpecialColumnService specialColumnService;

	public SpecialColumnComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<SpecialColumnInfo> queryList(String body, Head head) {
		SpecialColumnVo vo = gson.fromJson(body, SpecialColumnVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("isDelete", Constant.IS_NOT_DELETE);
			if (StringUtils.isNotBlank(vo.getOrderBy())) {
				if (Constant.ORDER_TYPE_ASC.equals(vo.getOrderType())) {
					map.put("page_orderByAsc", vo.getOrderBy());
				} else {
					map.put("page_orderByDesc", vo.getOrderBy());
				}
			}

			List<SpecialColumnInfo> list = specialColumnService.queryList(map);
			if (list != null && !list.isEmpty()) {
				for (SpecialColumnInfo col : list) {
					if (StringUtils.isNotBlank(col.getScolumnPath())) {
						col.setScolumnPath(CommonUtil.getUrlPrefix()
								+ col.getScolumnPath());
					}
				}
				return list;
			} else {
				return new ArrayList<SpecialColumnInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取专栏列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public SpecialColumnInfo querySpecialColumnDetail(String body, Head head) {
		SpecialColumnVo vo = gson.fromJson(body, SpecialColumnVo.class);
		if (vo != null) {
			String scolumnId = vo.getScolumnId();
			SpecialColumnInfo vv = specialColumnService
					.queryColumnDetail(scolumnId);
			if (vv != null) {
				return vv;
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",查询专栏详情信息时,没有查询到数据,专栏ID：" + scolumnId);
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取专栏详情信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean updateLike(String body, Head head) {
		SpecialColumnVo vo = gson.fromJson(body, SpecialColumnVo.class);
		if (vo != null) {
			String sid = vo.getScolumnId();
			SpecialColumnInfo ti = specialColumnService.queryColumnDetail(sid);
			if (ti != null) {
				Integer likeNum = ti.getLikeNum();
				if ("1".equals(vo.getLikeOrNot())) {
					likeNum = CommonUtil.add(likeNum, 1);
				} else if ("0".equals(vo.getLikeOrNot())) {
					likeNum = CommonUtil.sub(likeNum, 1);
				}
				SpecialColumnInfo sc = new SpecialColumnInfo();
				sc.setScolumnId(sid);
				sc.setLikeNum(likeNum);
				int re = specialColumnService.update(sc);
				if (re > 0) {
					return true;
				} else {
					return false;
				}
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",更新专栏点赞量时,没有查询到数据,专栏ID：" + sid);
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",更新专栏点赞量，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
